/**
 * import
 */
let form, layer, table,tableSelect , layedit , laydate , upload,$;
layui.use(['form','upload',  'tableSelect', 'layedit', 'laydate'], function () {
        form = layui.form;
        layer = layui.layer,
        tableSelect = layui.tableSelect,
        layedit = layui.layedit,
        laydate = layui.laydate,
        table = layui.table,
        upload = layui.upload ,
        $ = layui.jquery;
        let u = localStorage.getItem("a@m");
        let loginU={};
        if (u == undefined) {
            // layer.msg('请登录', {icon: 1, time: 500}, function () {
            //     window.location = 'page/login.html';
            // });
        }else{
            loginU=decryptLocalStorage(u);
        }

        //拖拽上传
        upload.render({
            elem: '#uploadFile'
            ,accept: 'file' //普通文件
            ,exts: 'xls|xlsx' //只允许上传压缩文件
            ,url: '/admin/v1/sport/importData'
            ,before: function(){
               this.data={token:loginU.token};
            }
            ,done: function(res){
                if(res.code==0){
                    $('#saveBtnArea').show();
                    setTimeout(function (){
                        let tableData=res.data;
                        loadTableData(tableData);
                        layer.msg('请查看数据预览！', {
                            icon: 1,
                            time: 2000
                        });;
                    },500);


                }else{
                    layer.msg(res.msg || '上传失败', {
                        icon: 2,
                        time: 2000
                    });
                }

            }
        });

        let errorCount=0;
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            if(errorCount>0)
            {
                layer.msg('数据有误，请把Excel表格数据修复后再上传', {icon: 2, time: 2000});
                return;
            }
            layer.confirm("确认保存数据吗？", {
                btn: ["确定", "取消"]
            }, function (index) {
                layer.close(index);
                request.otherGetMethod('user','batchAdd',{token:loginU.token},function(result){
                    if(result.code==0){
                        layer.msg('保存成功', {
                            icon: 1,
                            time: 500
                        },function () {
                            // 关闭弹出层
                            let iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        });

                    }else{
                        layer.msg(result.msg || '保存失败', {
                            icon: 2,
                            time: 2000
                        });
                    }
                });
            }, function (index) {
                layer.close(index);
            });

        });

        function loadTableData(data)
        {
            table.render({
                elem: '#importTable',
                data: data,
                cols: [[
                    {field: 'rowIndex0', align:"center", width: 80, title: '序号'},
                    {field: 'rowIndex1', align:"center", title: '账号'},
                    {field: 'rowIndex2', align:"center",  title: '姓名'},
                    {field: 'rowIndex3', align:"center",  title: '手机号'},
                    {field: 'rowIndex4', align:"center",  title: '性别'},
                    {field: 'rowIndex5', align:"center",  title: '角色名称'},
                    {field: 'errMsg', align:"center",  title: '数据描述'},
                ]],
                limits: [10, 15, 20, 25, 50, 100, 200, 300,500],
                limit: 20,
                page: true,
                skin: 'line',
                parseData: function (res) {
                    return res;
                },
                done: function(res, curr, count){
                    if(res.code===-1)
                    {
                        layer.msg('请登录', {icon: 1, time: 500},function () {
                            //window.location = 'logout';
                        });
                        return;
                    }
                    for(let i in res.data){
                        let item = res.data[i];
                        if(item.errMsg!='√')
                        {
                            errorCount++;
                            $("tr[data-index='" + i + "']").attr({"style":"color:red"});
                        }else{
                            $("tr[data-index='" + i + "']").attr({"style":"color:green"});
                        }

                    }
                }
            });
        }
});

